package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.UserAddressBookService;
import com.sky.service.impl.UserAddressBookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class UserAddressBookController {
    @Autowired
    private UserAddressBookServiceImpl userAddressBookService;

    @GetMapping("/default")
    public Result defaultAddressBook() {
        AddressBook addressBook = userAddressBookService.defaultAddressBook();
        return Result.success(addressBook);
    }

    @PutMapping("/default")
    public Result updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        userAddressBookService.updateDefaultAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List list = userAddressBookService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        AddressBook addressBook = userAddressBookService.getById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Integer id) {
        userAddressBookService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        userAddressBookService.update(addressBook);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        userAddressBookService.save(addressBook);
        return Result.success();
    }
}
